//
//  STSilentLivenessResultViewController.m
//  TestSTSilentLivenessController
//
//  Created by huoqiuliang on 2018/12/9.
//  Copyright © 2018 SenseTime. All rights reserved.
//

#import "STSilentLivenessResultViewController.h"
#import "STSilentLivenessCommon.h"
#import "UIView+EX.h"
@interface STSilentLivenessResultViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
@property (weak, nonatomic) IBOutlet UIImageView *resultImageView;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

@property (weak, nonatomic) IBOutlet UIButton *viewPhotoButton;

@property (weak, nonatomic) IBOutlet UIImageView *livenessImageView;
@property (weak, nonatomic) IBOutlet UIImageView *faceImageView;

@property (nonatomic, strong) NSString *bundlePathStr;
@end

@implementation STSilentLivenessResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"检测结果";
    self.edgesForExtendedLayout = UIRectEdgeNone;

    self.backgroundImageView.image = [UIImage imageNamed:@"background"];

    self.resultLabel.font = [UIFont systemFontOfSize:15.0f];
    self.resultLabel.numberOfLines = 0;
    self.resultLabel.textColor = kSTColorWithRGB(0xc7c7c7);
    self.resultLabel.textAlignment = NSTextAlignmentCenter;

    self.viewPhotoButton.backgroundColor = [UIColor blackColor];
    self.viewPhotoButton.layer.cornerRadius = 8;
    self.viewPhotoButton.layer.masksToBounds = YES;
    self.viewPhotoButton.layer.borderWidth = 1;
    self.viewPhotoButton.layer.borderColor = kSTColorWithRGBA(0xCC9233, 0.5).CGColor;
    self.viewPhotoButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.viewPhotoButton setTitle:@"查看照片" forState:UIControlStateNormal];
    [self.viewPhotoButton setTitleColor:kSTColorWithRGB(0xCC9233) forState:UIControlStateNormal];

    self.faceImageView.layer.borderWidth = 3.0;
    self.faceImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.faceImageView.backgroundColor = [UIColor blackColor];

    self.livenessImageView.image = self.livenessImage;
    self.faceImageView.image = self.faceImage;
    self.viewPhotoButton.hidden = NO;
    if (self.isSucceed) {
        self.resultImageView.image = [UIImage imageNamed:@"silentLivenessResultSuccess"];
        self.resultLabel.text = @"活体检测成功";
    } else {
        self.resultImageView.image = [UIImage imageNamed:@"silentLivenessResultFailed"];
        self.resultLabel.text = self.resultStr;
    }

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]
        initWithImage:[[UIImage imageNamed:@"navBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                style:UIBarButtonItemStylePlain
               target:self
               action:@selector(back)];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)back {
    if (self.livenessImageView.hidden && self.faceImageView.hidden) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        self.livenessImageView.hidden = YES;
        self.faceImageView.hidden = YES;
    }
}

- (IBAction)onViewPhotoButtonAction {
    self.livenessImageView.hidden = NO;
    self.faceImageView.hidden = (self.faceImage == nil);
}

- (UIImage *)imageWithFullFileName:(NSString *)fileName {
    NSString *filePath = [NSString pathWithComponents:@[self.bundlePathStr, @"images", fileName]];

    return [UIImage imageWithContentsOfFile:filePath];
}

@end
